<?php
class Articles_categories_model extends Model {
	
	/**
	 * Get child category
	 * @author tien.nguyen
	 * @param unknown_type $cateCode
	 */
	public function getChild($cateCode) {
		$this->db->select ( 'cat2.*' );
		$this->db->from ( 'news_categories' );
		$this->db->join ( 'news_categories as cat2', 'cat2.NewsCategoryParent = 
			news_categories.NewsCategoryId' );
		$this->db->where ( 'news_categories.NewsCategoryCode = "' . $cateCode . '"' );
		$query = $this->db->get ();
		return $query->result_array ();
	}
	
	/** 
	 * Get cate root
	 * @author tien.nguyen
	 */
	public function getCateRoot($cateCode) {
		$query = $this->db->get_where ( 'news_categories', array ('NewsCategoryCode' => $cateCode ) );
		$cate = $query->row_array ();
		if (! empty ( $cate )) {
			if (! empty ( $cate ['NewsCategoryParent'] )) {
				$query = $this->db->get_where ( 'news_categories', array ('NewsCategoryId' => $cate ['NewsCategoryParent'] ) );
				$cate = $query->row_array ();
			}
		}
		return $cate;
	}
	
	/**
	 * Get child cate for root
	 * @param string $cateCode
	 */
	public function getChildForRoot($cateCode) {
		$cate = $this->getCateRoot($cateCode);
		return $this->getChild ( $cate ['NewsCategoryCode'] );
	}
}